﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMDTO;
using IMBL;
using Ext.Net;

namespace IMPPFront.Management
{
    public partial class TCMeta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<IMDTORefCodeDetail> tcReferenties = IMBLMetadata.GetAllTechnischeCompetentieReferenties();
            List<IMDTORefCodeDetail> tcClusters = IMBLMetadata.GetAllTechnischeCompetentieClusters();
            List<IMDTORefCodeDetail> tcGidsen = IMBLMetadata.GetAllTechnischeCompetentieGidsen();
            List<IMDTOCompetentie> competenties = IMBLMetadata.GetAllTechnischeCompetenties();

            TCStore.DataSource = competenties;
            TCStore.DataBind();
            TCRStore.DataSource = tcReferenties;
            TCRStore.DataBind();
            TCRCombo.DataSource = tcReferenties;
            TCRCombo.DataBind();
            TCCStore.DataSource = tcClusters;
            TCCStore.DataBind();
            TCCCombo.DataSource = tcClusters;
            TCCCombo.DataBind();
            TCGStore.DataSource = tcGidsen;
            TCGStore.DataBind();
            TCGCombo.DataSource = tcGidsen;
            TCGCombo.DataBind();

        }

        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTOCompetentie> competenties = e.DataHandler.ObjectData<IMDTOCompetentie>();

            foreach (IMDTOCompetentie created in competenties.Created)
            {
                int tempId = created.idCompetentie;
                int newId = IMBLMetadata.AddCompetentie(created);
                if (TCStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    TCStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTOCompetentie deleted in competenties.Deleted)
            {
                IMBLMetadata.DeleteCompetentie(deleted.idCompetentie);

                if (TCStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idCompetentie.ToString());
                }
            }

            foreach (IMDTOCompetentie updated in competenties.Updated)
            {
                IMBLMetadata.UpdateCompetentie(updated);

                if (TCStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.idCompetentie.ToString());
                }
            }
            e.Cancel = true;
        }

        public void HandleRefCodeChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTORefCodeDetail> refcodes = e.DataHandler.ObjectData<IMDTORefCodeDetail>();
            Store senderStore = (Store)sender;

            foreach (IMDTORefCodeDetail created in refcodes.Created)
            {
                int tempId = created.idRefCodeDetail;
                int newId = IMBLMetadata.AddRefCodeDetail(created);
                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(tempId.ToString(), newId.ToString());
                }
                else
                {
                    senderStore.UpdateRecordId(tempId, newId);
                }

            }

            foreach (IMDTORefCodeDetail deleted in refcodes.Deleted)
            {
                IMBLMetadata.DeleteRefCodeDetail(deleted.idRefCodeDetail);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idRefCodeDetail.ToString());
                }
            }

            foreach (IMDTORefCodeDetail updated in refcodes.Updated)
            {
                IMBLMetadata.UpdateRefCodeDetail(updated);

                if (senderStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(updated.idRefCodeDetail.ToString());
                }
            }
            e.Cancel = true;
        }

        public void TCRStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> tcReferenties = IMBLMetadata.GetAllTechnischeCompetentieReferenties();
            TCRStore.DataSource = tcReferenties;
            TCRStore.DataBind();
            TCRCombo.DataSource = tcReferenties;
            TCRCombo.DataBind();
            
        }

        public void TCCStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> tcClusters = IMBLMetadata.GetAllTechnischeCompetentieClusters();
            TCCStore.DataSource = tcClusters;
            TCCStore.DataBind();
            TCCCombo.DataSource = tcClusters;
            TCCCombo.DataBind();
        }

        public void TCGStore_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            List<IMDTORefCodeDetail> tcGidsen = IMBLMetadata.GetAllTechnischeCompetentieGidsen();
            TCGStore.DataSource = tcGidsen;
            TCGStore.DataBind();
            TCGCombo.DataSource = tcGidsen;
            TCGCombo.DataBind();
        }

        public void GidsAddButton_Clicked(object sender, DirectEventArgs e)
        {
            if (TCSelectionModel.SelectedRows.Count.Equals(0))
            {
                X.Msg.Alert("Waarschuwing!", "Selecteer A.U.B. eerst een rij").Show();
            }
            else if (TCSelectionModel.SelectedRows.Count > 1)
            {
                X.Msg.Alert("Waarschuwing!", "U mag maar één rij selecteren").Show();
            }
            else
            {
                int tcId = int.Parse(TCSelectionModel.SelectedRows.First().RecordID);
                GidsIdCompetentie.Text = tcId.ToString();
                List<IMDTORefCodeDetail> availableGidsen = IMBLMetadata.GetAllTechnischeCompetentieGidsen();
                List<IMDTORefCodeDetail> submittedGidsen = IMBLMetadata.GetGidsenForTechnischeCompetentie(tcId);
                List<int> ids = (from sg in submittedGidsen
                                 select sg.idRefCodeDetail).ToList();

                availableGidsen = availableGidsen.Where(x => !ids.Contains(x.idRefCodeDetail)).ToList();

                AvailableGidsenStore.DataSource = availableGidsen;
                AvailableGidsenStore.DataBind();
                SubmitGidsenStore.DataSource = submittedGidsen;
                SubmitGidsenStore.DataBind();
                GidsAddWindow.Show();
            }
        }

        public void HandleGidsChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            ChangeRecords<IMDTORefCodeDetail> refcodes = e.DataHandler.ObjectData<IMDTORefCodeDetail>();
            int idCompetentie = int.Parse(GidsIdCompetentie.Text);
            X.Msg.Notify("Starting", "starting to update competentie " + idCompetentie);
            foreach (IMDTORefCodeDetail created in refcodes.Created)
            {
                
                int tempId = created.idRefCodeDetail;
                int gidsId = created.refCodeDetail;
                X.Msg.Notify("adding", "adding gids" + gidsId + " to competentie " + idCompetentie).Show();
                IMBLMetadata.AddGidsToCompetentie(gidsId, idCompetentie);

                e.ConfirmationList.ConfirmRecord(tempId.ToString());

            }

            foreach (IMDTORefCodeDetail deleted in refcodes.Deleted)
            {
                IMBLMetadata.RemoveGidsFromCompetentie(deleted.refCodeDetail, idCompetentie);

                if (SubmitGidsenStore.UseIdConfirmation)
                {
                    e.ConfirmationList.ConfirmRecord(deleted.idRefCodeDetail.ToString());
                }
            }

            e.Cancel = true;
        }
    }
}